<?php
/**
 * dao抽象类
 *
 * @author singal
 */
abstract class Kit_Dao_Abstract {

	/**
	 * 表名字
	 *
	 * @var string
	 */
	protected $_table='';
	
	/**
	 * 数据库名(不分库，值为空)
	 * 
	 * @var string
	 */
	protected $_data='';
	
	/**
	 * 数据访问源服务标识
	 * 
	 * @var string
	 */
	protected $_identity='x01';

	/**
	 * construct
	 */
	public function __construct(){
		$this->init();
	}

	/**
	 * 初始操作
	 *
	 * @return void
	 */
	abstract protected function init();

	/**
	 * 获取数据访问源
	 *
	 * @return Kit_Db_Adapter
	 */
	public function getAdapter(){
		return Kit_Db_Manager::getAdapter($this->_identity);
	}
	
	/**
	 * 设置访问源标识(为分布式服务器提供)
	 * 
	 * @param String $identity
	 * @return void
	 */
	public function setAdapter($identity){
		if($identity){
			$this->_identity = $identity;
		}
	}
	
	/**
	 * 设置访问表明(为分表分库提供)
	 * 
	 * @param string $table
	 * @return void
	 */
	public function setTableName($table){
		if($table){
			$this->_table = $table;
		}
	}
	
	/**
	 * 设置访问的数据库名(为分表分库提供)
	 * 
	 * @param unknown_type $data
	 */
	public function setDbName($data){
		if($data){
			$this->_data = $data;
		}
	}

	/**
	 * 获取表名字
	 *
	 * @return string
	 */
	public function getTableName(){
		$return = '';
		if($this->_table){
			if($this->_data == ''){
				$return = $this->_table;
			}else{
				$return = $this->_data.'.'.$this->_table;
			}
		}else{
			throw new Kit_Db_Exception('table name can\'t empty', 504);
		}
		return $return;
	}
	
	/**
	 * 获取列表
	 * 
	 * @param string|array $where
	 * @param string $order
	 * @param int $page
	 * @param int $psize
	 * @return array
	 */
	public function getItems($where, $order, $page=0, $psize=10){
		$return = array();
		if($where){
			$query = 'SELECT * FROM ';
			$query .= $this->getTableName();
			$query .= ' WHERE '.$this->getWhere($where);
			if($order){
				$query .= ' ORDER BY '.$order;
			}
			$page = $page>0?$page:1;
			$psize = $psize>0?$psize:10;
			$offset = ($page-1)*$psize;
			$query .= ' LIMIT '.$offset.','.$psize;
			$return = $this->getAdapter()->queryAll($query);
		}
		return $return;
	}

	/**
	 * 删除记录
	 *
	 * @param string|array $where
	 * @return int
	 */
	public function delete($where){
		$return = 0;
		if($where){
			$query = 'DELETE FROM ';
			$query .= $this->getTableName();
			$query .= ' WHERE ';
			$query .= $this->getWhere($where);
			$return = $this->getAdapter()->queryUpdate($query);
		}
		return $return;
	}

	/**
	 * 获取单条记录
	 *
	 * @param string|array $where
	 * @return array
	 * @throws Kit_Db_Exception
	 */
	public function getItem($where, $sort=''){
		$return  = array();
		if($where){
			$query = 'SELECT * FROM ';
			$query .= $this->getTableName();
			$query .= ' WHERE '.$this->getWhere($where);
			if($sort != ''){
				$query .= ' ORDER BY '.$sort;
			}
			$query .= ' LIMIT 0,1';
			$return = $this->getAdapter()->queryRow($query);
		}
		return $return;
	}

	/**
	 * 获取表字段信息
	 *
	 * @return array
	 */
	public function getTableFields(){
		$query = 'SHOW COLUMNS FROM ';
		$query .= $this->getTableName();
		return $this->getAdapter()->queryAll($query);
		
	}
	
	/**
	 * 统计满足条件记录数量
	 * 
	 * @param string|Array $where
	 * @return int
	 */
	public function getWhereTotal($where){
		$return = 0;
		if($where){
			$sql = 'SELECT COUNT(*) FROM '.$this->getTableName();
			$sql .= ' WHERE '.$this->getWhere($where);
			$return = $this->getAdapter()->queryTotal($sql);
		}
		return $return;
	}

	/**
	 * 修改数据
	 *
	 * @param string|array $where
	 * @param array $set
	 * @return int
	 */
	public function update($where, array $set){
		$return = 0;
		if($where && $set){
			$query = 'UPDATE '.$this->getTableName();
			$query .= ' SET '.$this->getSet($set);
			$query .= ' WHERE '.$this->getWhere($where);
			if($query){
				$return  = $this->getAdapter()->queryUpdate($query);
			}
		}
		return $return;
	}
	
	/**
	 * 增加单条数据记录
	 * 
	 * @param array $record 
	 * @return int
	 */
	public function add(array $row){
		$return = 0;
		if($row){
			$keys = '';
			$values = '';
			foreach($row as $key=>$value){
				$keys .= $key.',';
				if(substr($key, 0, 1) == 'n'){
					$values .= $value.',';
				}else{
					$values .= "'".$value."',";
				}
			}
			$keys = substr($keys, 0, -1);
			$values = substr($values, 0, -1);
			$query = 'INSERT INTO ';
			$query .= $this->getTableName();
			$query .= '('.$keys.') VALUES(';
			$query .= $values.')';
			$return = $this->getAdapter()->queryUpdate($query);
		}
		return $return;
	}
	
	/**
	 * 格式化where语句
	 * 
	 * @param string|array $where
	 * @return string
	 */
	protected function getWhere($where){
		$return = '';
		if(is_array($where)){
			$return .= implode(' AND ', $where);
		}else{
			$return .= $where;
		}
		return $return;
	}
	
	/**
	 * 格式化成更新语句格式
	 * 
	 * @param array $set
	 * @return String
	 */
	protected function getSet(array $set){
		$return = '';
		if($set){
			foreach ($set as $key=>$val){
				if (substr($key, 0, 1) != 'n'){
					$return .= $key."='".$val."',";
				}else{
					$return .= $key."=".$val.",";
				}
			}
			$return = substr($return, 0, -1);
		}
		return $return;
	}
}
//end